﻿//function valid(id)
//{
//    var valid = $(id).Validform({
//        btnSubmit: "a.submit",
//        showAllError: true,
//        tiptype: function (msg, o, cssctl)
//        {
//            if (o.obj.next('p.Validform_checktip').length == 0)
//                o.obj.after('<p class="Validform_checktip"></p>');
//            var d = o.obj.next('p.Validform_checktip');
//            if (o.type != 2)
//            {
//                d.addClass('Validform_wrong').html(msg);
//            } else
//            {
//                d.removeClass('Validform_wrong').html('');
//            }
//        },
//        datatype: {
//            "zp2-4": /^[a-zA-Z]{1,20}|[\u4e00-\u9fa5]{2,4}$/,
//            "zh1-6": /^[\u4E00-\u9FA5\uf900-\ufa2d]{1,5}$/
//        }
//    });
//}


/*  ============================================================
 *  非弹窗报价器-日期部分
 */
(function ($, doc, $$) {

    $.init();
    var tomorrow = moment().add(1, 'days').format('YYYY-MM-DD');
    var tomorrow_nd = new Date(tomorrow.replace(/-/g, "/"));
    var pickers = $('.dtpicker'), fromtime_nd = tomorrow_nd, totime_nd, fromtime = tomorrow, totime;

    //@* Add Start by Kevin 2018/02/06 IBT new product *@

    var ibtDateEnd = tomorrow;
    var ibtDateEnd_nd = tomorrow_nd;
    if ($$("#ProductCode").val() == "9047") {
        ibtDateEnd = moment($$("#FrmEndDate").val()).format('YYYY-MM-DD');
        ibtDateEnd_nd = new Date(ibtDateEnd.replace(/-/g, "/"));
        $$('.num-days').html('共365天');
        pickers = $('.dtpicker'), fromtime_nd = tomorrow_nd, totime_nd = ibtDateEnd_nd, fromtime = tomorrow, totime = ibtDateEnd;
    }
    //@* Add End by Kevin 2018/02/06 IBT new product *@

    pickers.each(function (i, o) {
        var $this = $$(this);
        var _this = this;
        var name = _this.getAttribute('name');
        $$('input[name=' + name + ']').val((name == 'plan_fromtime' ? '开始' : '结束') + '  ' + tomorrow).attr('data-date', tomorrow);

        //@* Add Start by Kevin 2018/02/06 IBT new product *@
        if ($$("#ProductCode").val() == "9047") {
            $$('input[name=' + name + ']').val((name == 'plan_fromtime' ? '开始' + '  ' + tomorrow : '结束' + '  ' + ibtDateEnd)).attr('data-date', (name == 'plan_fromtime' ? tomorrow : ibtDateEnd));
        }
        //@* Add End by Kevin 2018/02/06 IBT new product *@

        o.addEventListener('click', function () {
            $this.blur();
            var dtpicker;
            if (name == 'plan_fromtime') {
                dtpicker = new mui.DtPicker({
                    type: 'date',
                    buttons: ['取消', '完成'],
                    beginDate: tomorrow_nd
                })
                dtpicker.show(function (e) {
                    $$('.num-days').html('共1天');

                    fromtime = e.text;
                    _this.value = '开始  ' + fromtime;
                    _this.setAttribute('data-date', fromtime);

                    if ($$("#ProductDuration").val() == "") {
                        //$$('input[name=plan_totime]').val('结束  ' + fromtime).attr('data-date', fromtime);
                        var d1 = moment(fromtime).format('YYYYMMDD') * 1;
                        var d2 = moment($$('input[name=plan_totime]').attr("data-date")).format('YYYYMMDD') * 1;

                        if (d1 > d2) {
                            $$('input[name=plan_totime]').val('结束  ' + fromtime).attr('data-date', fromtime);
                        }

                        var n = (moment(new Date($$('input[name=plan_totime]').attr("data-date"))).diff(new Date(fromtime), 'days') + 1) * 1;
                        $$('.num-days').html('共' + n + '天');
                    }
                    else {
                        var productDuration = $$("#ProductDuration").val() * 1 - 1;
                        var dateend = moment(new Date(fromtime)).add(productDuration, 'days').format('YYYY-MM-DD');
                        // endPicker.setMaxDate(new Date(moment(dateend).format('YYYY'), moment(dateend).add(-1, 'month').format('MM'), moment(dateend).format('DD')));

                        var n = (moment(new Date($$('input[name=plan_totime]').attr("data-date"))).diff(new Date(fromtime), 'days') + 1) * 1;
                        var d1 = moment(fromtime).format('YYYYMMDD') * 1;
                        var d2 = moment($$('input[name=plan_totime]').attr("data-date")).format('YYYYMMDD') * 1;

                        if (d1 > d2 || n > $$("#ProductDuration").val() * 1) {
                            //$('#dateend').val(moment(startDate).format('YYYY-MM-DD'));
                            $$('input[name=plan_totime]').val('结束  ' + fromtime).attr('data-date', fromtime);
                        }
                        else {
                            $$('.num-days').html('共' + n + '天');
                        }
                    }

                    //@* Add Start by Kevin 2018/02/06 IBT new product *@
                    if ($$("#ProductCode").val() == "9047") {
                        var ibtDateEnd = moment(new Date(fromtime)).add(364, 'days').format('YYYY-MM-DD');
                        $$('input[name=plan_totime]').val('结束  ' + ibtDateEnd).attr('data-date', ibtDateEnd);
                        $$('.num-days').html('共365天');
                    }
                    //@* Add End by Kevin 2018/02/06 IBT new product *@

                    fromtime_nd = new Date(fromtime.replace(/-/g, "/"));
                    //totime_nd   = new Date(totime.replace(/-/g, "/"));

                    dtpicker.dispose();

                    $$("#FrmStartDate").val(fromtime);
                    $$("#FrmEndDate").val($$('input[name=plan_totime]').attr("data-date"));

                    insure();
                });
            }
            else {
                $$('.item-tip.daterange-tip').hide();

                if ($$("#ProductDuration").val() != "") {
                    var duration = $$("#ProductDuration").val() * 1 - 1;
                    totime = moment(fromtime).add(duration, 'days').format('YYYY-MM-DD');

                    totime_nd = new Date(totime.replace(/-/g, "/"));

                    dtpicker = new mui.DtPicker({
                        type: 'date',
                        buttons: ['取消', '完成'],
                        beginDate: fromtime_nd,
                        endDate: totime_nd
                    })
                }
                else {
                    totime = moment(fromtime).add(181, 'days').format('YYYY-MM-DD');
                    totime_nd = new Date(totime.replace(/-/g, "/"));

                    dtpicker = new mui.DtPicker({
                        type: 'date',
                        buttons: ['取消', '完成'],
                        beginDate: fromtime_nd,
                        endDate: totime_nd
                    })
                }

                dtpicker.show(function (e) {
                    _this.value = '结束  ' + e.text;
                    _this.setAttribute('data-date', e.text);
                    var days = moment(e.text).diff(fromtime, 'days') + 1;
                    $$('.num-days').html('共' + days + '天');
                    dtpicker.dispose();

                    $$("#FrmStartDate").val($$('input[name=plan_fromtime]').attr("data-date"));
                    $$("#FrmEndDate").val(e.text);

                    insure();
                });
            }
        }, false);
    });
})(mui, document, jQuery);

$(function () {
    /*==============================
     *  报价器：
     *  立即报价按钮显示效果
     */
    $(window).scroll(function () {
        if ($('.plan-form').length == 1) {
            var top = $(document).scrollTop();
            var itemsTop = $('#a_GoToBuy').offset().top;
            if (top > itemsTop - 30) {
                $('.plan-form .tit-continue').fadeIn().addClass('b-submit');
            } else
                $('.plan-form .tit-continue').hide().removeClass('b-submit');
        }
    });

    /*==============================
     *  弹窗：
     *  报价器
     */
    $(document).on('click', '#plan.shadow a.tit', function () {
        $(this).parent().toggleClass('layer');
        if ($('.layui-m-layer').length == 1) {
            $('.layui-m-layer').remove();
            $('body').removeClass('lock-position');
            return;
        }

        $('#plan').removeAttr('style', '');

        layer.open({
            type: 1
            , shade: 0
            , content: $('section.plan-form').prop("outerHTML")
            , anim: 'down'
            , style: 'position:fixed; left:0; top:0; width:100%; height:100%; border: none; -webkit-animation-duration: .3s; animation-duration: .3s;'
            , success: function () {
                $('body').addClass('lock-position');
                var i_data = $('section.plan-form .form-box');
                $('.layui-m-layer input[name=plan_city]').val(i_data.find('input[name=plan_city]').val());
                //update by lisa 20170606 取儿童人数和成人人数不从select取，取隐藏域的data-val
                var adult = i_data.find('input[name=plan_adult]').attr("data-val"),
                    child = i_data.find('input[name=plan_child]').attr("data-val"),
                //var adult = i_data.find('input[name=plan_adult]').next().val(),
                //    child = i_data.find('input[name=plan_child]').next().val(),
                    fromtime = i_data.find('input[name=plan_fromtime]').attr('data-date');
                totime = i_data.find('input[name=plan_totime]').attr('data-date');
                $('.layui-m-layer input[name=plan_adult]').val('成人数  ' + adult).next().val(adult);
                $('.layui-m-layer input[name=plan_child]').val('儿童数  ' + child).next().val(child);
                $('.layui-m-layer input[name=plan_fromtime]').val(fromtime ? ('开始  ' + fromtime) : '').attr('data-date', fromtime);
                $('.layui-m-layer input[name=plan_totime]').val(totime ? ('结束  ' + totime) : '').attr('data-date', totime);

                //if (i_data.find('input[name=plan_city]').val() != '')
                //{
                //    $('.layui-m-layer a.b-continue').attr('style', 'background-color: #fe8a01;');
                //}
                //else
                //{
                //    $('.layui-m-layer a.b-continue').removeAttr('style');
                //}

                (function ($, doc, $$) {
                    $.init();
                    var tomorrow = moment().add(1, 'days').format('YYYY-MM-DD');
                    var tomorrow_nd = new Date(tomorrow.replace(/-/g, "/"));
                    var pickers = $('.layui-m-layer .dtpicker'), fromtime_nd = tomorrow_nd, totime_nd, fromtime = tomorrow, totime;

                    pickers.each(function (i, o) {
                        var _this = this;
                        var name = _this.getAttribute('name');
                        //$$('.layui-m-layer input[name=' + name + ']').val((name == 'plan_fromtime' ? '开始' : '结束') + '  ' + tomorrow).attr('data-date', tomorrow);

                        o.addEventListener('click', function () {
                            var dtpicker;
                            if (name == 'plan_fromtime') {
                                dtpicker = new mui.DtPicker({
                                    type: 'date',
                                    buttons: ['取消', '完成'],
                                    beginDate: tomorrow_nd
                                })

                                dtpicker.show(function (e) {
                                    $$('.num-days').html('共1天');

                                    fromtime = e.text;
                                    _this.value = '开始  ' + fromtime;
                                    _this.setAttribute('data-date', fromtime);

                                    $$('input[name=plan_fromtime]').val('开始  ' + fromtime).attr('data-date', fromtime);

                                    if ($$("#ProductDuration").val() == "") {
                                        //$$('input[name=plan_totime]').val('结束  ' + fromtime).attr('data-date', fromtime);
                                        var d1 = moment(fromtime).format('YYYYMMDD') * 1;
                                        var d2 = moment($$('.layui-m-layer input[name=plan_totime]').attr("data-date")).format('YYYYMMDD') * 1;

                                        if (d1 > d2) {
                                            $$('input[name=plan_totime]').val('结束  ' + fromtime).attr('data-date', fromtime);
                                        }

                                        var n = (moment(new Date($$('.layui-m-layer input[name=plan_totime]').attr("data-date"))).diff(new Date(fromtime), 'days') + 1) * 1;
                                        $$('.num-days').html('共' + n + '天');
                                    }
                                    else {
                                        var productDuration = $$("#ProductDuration").val() * 1 - 1;
                                        var dateend = moment(new Date(fromtime)).add(productDuration, 'days').format('YYYY-MM-DD');
                                        // endPicker.setMaxDate(new Date(moment(dateend).format('YYYY'), moment(dateend).add(-1, 'month').format('MM'), moment(dateend).format('DD')));

                                        var n = (moment(new Date($$('.layui-m-layer input[name=plan_totime]').attr("data-date"))).diff(new Date(fromtime), 'days') + 1) * 1;
                                        var d1 = moment(fromtime).format('YYYYMMDD') * 1;
                                        var d2 = moment($$('.layui-m-layer input[name=plan_totime]').attr("data-date")).format('YYYYMMDD') * 1;

                                        if (d1 > d2 || n > $$("#ProductDuration").val() * 1) {
                                            //$('#dateend').val(moment(startDate).format('YYYY-MM-DD'));
                                            $$('input[name=plan_totime]').val('结束  ' + fromtime).attr('data-date', fromtime);
                                        }
                                        else {
                                            $$('.num-days').html('共' + n + '天');
                                        }
                                    }

                                    fromtime_nd = new Date(fromtime.replace(/-/g, "/"));
                                    //totime_nd = new Date(totime.replace(/-/g, "/"));

                                    dtpicker.dispose();

                                    $$("#FrmStartDate").val(fromtime);
                                    $$("#FrmEndDate").val($$('.layui-m-layer input[name=plan_totime]').attr("data-date"));

                                    insure();
                                });
                            }
                            else {
                                $$('.item-tip.daterange-tip').hide();

                                if ($$("#ProductDuration").val() != "") {
                                    var duration = $$("#ProductDuration").val() * 1 - 1;
                                    totime = moment(fromtime).add(duration, 'days').format('YYYY-MM-DD');
                                    totime_nd = new Date(totime.replace(/-/g, "/"));

                                    dtpicker = new mui.DtPicker({
                                        type: 'date',
                                        buttons: ['取消', '完成'],
                                        beginDate: fromtime_nd,
                                        endDate: totime_nd
                                    })
                                }
                                else {
                                    totime = moment(fromtime).add(181, 'days').format('YYYY-MM-DD');
                                    totime_nd = new Date(totime.replace(/-/g, "/"));

                                    dtpicker = new mui.DtPicker({
                                        type: 'date',
                                        buttons: ['取消', '完成'],
                                        beginDate: fromtime_nd,
                                        endDate: totime_nd
                                    })
                                }

                                dtpicker.show(function (e) {
                                    _this.value = '结束  ' + e.text;
                                    _this.setAttribute('data-date', e.text);

                                    $$('input[name=plan_totime]').val('结束  ' + e.text).attr('data-date', e.text);

                                    var days = moment(e.text).diff(fromtime, 'days') + 1;
                                    $$('.num-days').html('共' + days + '天');
                                    dtpicker.dispose();

                                    $$("#FrmStartDate").val($$('.layui-m-layer input[name=plan_fromtime]').attr("data-date"));
                                    $$("#FrmEndDate").val(e.text);

                                    insure();
                                });
                            }
                        }, false);
                    });
                })(mui, document, jQuery);
            }
        });

        //Plan code
        $('.layui-m-layermain .it-plan .radio-box a').click(function () {
            var pi = $(this).closest('ul').index();
            var i = $(this).parent().index();

            $('.security-v2 .ensure-tab-box dl:eq(' + pi + ') dd:eq(' + i + ') a').click();


        })

        //insure();
    });

    /*
     *  国家选择窗口显示
     */
    var layer_country;
    $(document).on('click', 'input[name=plan_city]', function () {
        $(this).blur();
        //$('body').addClass('lock-position');
        //$('#tpl-country .countryBox').show();

        $('body').addClass('lock-position');
        layer_country = layer.open({
            type: 1
            , shade: 0
            , content: '<div class="countryBox" id="divCountryWindow">' + $('#tpl-country .countryBox').html() + '</div>'
            , anim: 'up'
            , style: 'position:fixed; left:0; top:0; width:100%; height:100%; border: none; -webkit-animation-duration: .3s; animation-duration: .3s;'
        });
    });

    /*
     *  国家所属洲切换
     */
    $(document).on('click', '#divCountryWindow.countryBox .country-tab a', function () {
        var i = $(this).parent().index();
        $(this).addClass('on').parent().siblings().find('a').removeClass('on');
       
        if (IsByteDancePartner.toLowerCase() == "true") {
            $('#divCountryWindow.countryBox .country-cont .cont:eq(' + i + ')').css("cssText", "display:block !important").show().siblings().hide()
        } else {
            $('#divCountryWindow.countryBox .country-cont .cont:eq(' + i + ')').show().siblings().hide();
        }
    })
    /*
     *  国家点击选择
     */
    $(document).on('click', '.countryBox .country-cont .cont dd a', function () {
        if ($('.layui-m-layer .plan-form').length == 0) {
            $('body').removeClass('lock-position');
        }
        $('input[name=plan_city]').val($(this).html()).removeClass('Validform_wrong'); //.blur();
        //$('.countryBox').hide();

        layer.close(layer_country);

        // 国家变更，获取国家对应产品信息ProductCode、最低报价计划、产品保障天数
        if ($(this).html() != "") {
            $("#FrmAddress").val($(this).html());
            //产品页面目的地改变以后需要清空上一个目的地选中的计划状态 给万国游产品专用
            var isProduct = $("#hidIsProduct").val();
            if (isProduct == "true" && $("#ProductCode").val() == "9111") {
                clearPlanSelectionStatus();
            }
            GetProductByCountry();
            //var isProduct = $("#hidIsProduct").val();

            //if (isProduct != "true")
            //{
            //    GetProductByCountry(insure);
            //}
        }
    })
    /*
     *  国家选择窗口关闭
     */
    $(document).on('click', '.countryBox a.close', function () {
        if ($('.layui-m-layer .plan-form').length == 0) {
            $('body').removeClass('lock-position');
        }
        //$('.countryBox').hide();
        layer.close(layer_country);
    })
    /*
     *  单选框
     */
    $(document).on('click', '.radio-box>ul>li>a', function () {
        if ($(this).hasClass('disabled'))
            return false;
        var v = $(this).attr('data-val');
        //不要去掉所有的选中状态，去掉当前的父级对象的选中的状态。
        $(this).parents("ul").find("a").removeClass('selected');
        //$(this).closest('.radio-box').find('ul>li>a').removeClass('selected');
        $(this).closest('.radio-box').find('input').val(v);
        $(this).addClass('selected');

        $("#FrmPlanCode").val(v);

        //insure();
    })
    /*
     *  Select赋值
     */
    $(document).on('change', 'select', function () {
        if ($(this).val() == 0 && $(this).attr('name') != 'pserson_num') {
            $(this).siblings('input').val('请选择').addClass('Validform_wrong');
            return false;
        }

        $(this).siblings('input').val($(this).find('option:selected').text()).blur();

        if ($(this).siblings('input').hasClass("Validform_wrong")) {
            $(this).siblings('input').removeClass('Validform_wrong').blur();
        }

        //get new plan


        //GetProductByCountry();
    })
    /*
     *  国家、人头数、日期验证
     */
    $(document).on('change', 'section input,.layui-m-layer .plan-form input,select', function () {
        var $this = $(this);
        var v = $this.val();

        var $step = $this.parents('.plan-form');

        if (v == '') {
            valiform(1, $this);
            return false;
        }

        if ($this.attr('rel') == 'adult') {
            var adultValue = $this.children('option:selected').val();

            $('input[name=plan_adult]').val('成人数  ' + adultValue).attr('data-val', adultValue);
            $step.find('input[name=plan_adult]').val('成人数  ' + adultValue).attr('data-val', adultValue);

            $("#FrmAdultNum").val(adultValue * 1);
            $("#FrmChildNum").val($step.find('input[name=plan_child]').attr('data-val') * 1);

            var num = adultValue * 1 + $step.find('input[name=plan_child]').attr('data-val') * 1;
            //更改总人数需要放在 方法return false 之前，不然总人数不能刷新
            $('.num-person').html('共' + num + '人').removeAttr("style");
            $step.find('.num-person').html('共' + num + '人').removeAttr("style");
            if (num == 0) {
                $('.num-person').html('请选择一个旅行人员').attr("style", "color:red;");
                $step.find('.num-person').html('请选择一个旅行人员').attr("style", "color:red;");
                //0人的时候需要清空价格
                $('.plan-tit').html('<a href="javascript:;" class="tit">定制保险计划</a>');
                return false;
            }


        }

        if ($this.attr('rel') == 'child') {
            var childValue = $this.children('option:selected').val();

            $('input[name=plan_child]').val('儿童数  ' + childValue).attr('data-val', childValue);
            $step.find('input[name=plan_child]').val('儿童数  ' + childValue).attr('data-val', childValue);


            $("#FrmAdultNum").val($step.find('input[name=plan_adult]').attr('data-val') * 1);
            $("#FrmChildNum").val(childValue * 1);

            var num = childValue * 1 + $step.find('input[name=plan_adult]').attr('data-val') * 1;
            //更改总人数需要放在 方法return false 之前，不然总人数不能刷新
            $('.num-person').html('共' + num + '人').removeAttr("style");
            $step.find('.num-person').html('共' + num + '人').removeAttr("style");
            if (num == 0) {
                $('.num-person').html('请选择一个旅行人员').attr("style", "color:red;");
                $step.find('.num-person').html('请选择一个旅行人员').attr("style", "color:red;");
                //0人的时候需要清空价格
                $('.plan-tit').html('<a href="javascript:;" class="tit">定制保险计划</a>');
                return false;
            }
        }

        //if ($("#ProductCode").val() == "9068") {
        if ($("#ProductCode").val() == "9111") {
            GetProductByCountry();
        }
        //add by lisa 20180125 首页的乐悠游需要根据儿童变化人数来切换计划 start
        if ($("#ProductCode").val() == "9112" && $("#hidIsProduct").val() != "true") {
            GetProductByCountry();
        }
        //add by lisa 20180125 首页的乐悠游需要根据儿童变化人数来切换计划 start
        else {
            insure();
        }

        //if ($('.plan-form').length > 0 && $('.plan-form input[name=plan_city]').val() != '')
        //$('.plan-tit').html('<a href="javascript:;" class="tit">总保费 <em>600</em> 元起</a><a href="order.html" class="tit-continue">立即投保</a>');
    });
    /*
     *  输入错误提示时，再次输入点击去掉错误
     */
    $(document).on('focus', 'input.Validform_wrong', function () {
        valiform(0, $(this));
    })
})
 
function valiform(type, obj) {
    if (type == 1) {
        obj.addClass('Validform_wrong').val(obj.attr('nullmsg'));
    } else if (type == 2) {
        obj.addClass('Validform_wrong').val(obj.attr('errormsg'));
    } else if (type > 2) {
        obj.addClass('Validform_wrong').val(obj.attr('errormsg' + (type - 1)));
    } else {
        obj.removeClass('Validform_wrong').val('');
    }
}

// <!--- Add Start by Kevin 2018/04/12 optimize mobile product homepage --> 
function scroll(fn) {
    var beforeScrollTop = document.body.scrollTop,
        fn = fn || function () { };
    window.addEventListener("scroll", function () {
        var afterScrollTop = document.body.scrollTop,
            delta = afterScrollTop - beforeScrollTop;
        if (delta === 0) return false;
        fn(delta > 0 ? "down" : "up");
        beforeScrollTop = afterScrollTop;
    }, false);
}

$(function () {
    scroll(function (direction) {
        if (direction == 'down') {
            $('#gotop').hide().attr('style', 'display:none;z-index:9;');
        }
        if (direction == 'up') {
            if (document.body.scrollTop < 400) {
                $('#gotop').hide().attr('style', 'display:none;z-index:9;');
                return false;
            }
            $('#gotop').show('fast');
        }
    });
    $(document).on('click', '#gotop', function () {
        $('body,html').animate({
            scrollTop: 0
        },
        1000);
        return false;
    })
})
// <!--- Add End by Kevin 2018/04/12 optimize mobile product homepage -->  